/**
 * @FileName: SplashImage.java
 * @Author
 * @Description:
 * @Date 2017年2月8日 上午10:54:43
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.validator.admin;

import java.util.Date;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

import cn.hpclub.server.model.SplashImage;

public class SplashImageValidator extends Validator{

    @Override
    protected void validate(Controller c){
        validateRequiredString("splashImage.title", "imageNameMessages", "启动页名称不能为空");
        validateRequiredString("splashImage.img_url", "imageUrlMessages", "启动页图片URL不能为空");
        validateCreateTime(c);
        String imageSize = c.getPara("splashImage.img_size");
        if(Integer.parseInt(imageSize) > 1024){
            addError("imageSizeMessages", "图片大小不能超过1024KB，请重新重新选择图片");
        }
    }

    private void validateCreateTime(Controller c){
        validateRequired("splashImage.start_date", "imageStartDateMessages", "开始时间不能为空");
        validateRequired("splashImage.end_date", "imageEndDateMessages", "结束时间不能为空");
        Date startTime = c.getParaToDate("splashImage.start_date");
        Date endTime = c.getParaToDate("splashImage.end_date");

        if(endTime.before(startTime)){// 结束时间还比开始时间晚
            addError("imageEndDateMessages", "结束时间不能晚于开始时间，请重新选择");
        }
    }

    @Override
    protected void handleError(Controller c){
        c.setAttr("enabled", c.getPara("splashImage.enabled"));
        c.keepModel(SplashImage.class);
        c.render("/admin/splash_image_input.tpl");
    }

}
